<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@ include file="/web/header.jsp" %>
<title>Insert title here</title>
</head>
<body>
<fieldset class="layui-elem-field">
 <legend>部门添加</legend>
 <div class="layui-field-box">
	<form class="layui-form layui-form-pane" lay-filter="formA">
	 <div class="layui-form-item">
	  <label class="layui-input-label">编号 </label>
	  <div class="layui-input-inline">
	   <input type="text" name="code" required readonly
	   	placeholder="请输入编号" autocomplete="off" class="layui-input">
	  </div>
	 </div>
	 <div class="layui-form-item">
	  <label class="layui-input-label">名称</label>
	  <div class="layui-input-inline">
	  <select name='codeDept'></select>
	   </div>
	 </div>
	 <div class="layui-form-item">
	  <div class="layui-input-block">
	 	<input type="button" value='确定' class="layui-btn"lay-submit lay-filter="setDept">
	 	<input type="button" value='关闭' class="layui-btn" onclick="layerClose()">
	  </div>
	 </div>
	 <input type="hidden" name="action" value="setDept">
	</form>
</div>
</fieldset>
<script type="text/javascript">
var code='<%= request.getParameter("code") %>';
	
	console.log("code="+code);
	function init(){
		ajax("/EmployeeServlet",{action:"get",code:code},'json',function(data){
			console.log(data)
			form.val("formA",{code:data.code,codeDept:data.codeDept})
		});
	}
	initSel();
	function initSel(){
		ajax('/DepartmentServlet',{action:'list'},'json',function(data){
			var opt="<option value=''>请选择部门</option>"
			$.each(data,function(){
				opt+="<option value='"+this.code+"'>"+this.name+"</option>"
			})
			$("select[name='codeDept']").html(opt);
			form.render();
			init()
		})
	}

	formOnSubmit('setDept','/EmployeeServlet','text',function(data){
		if(1==data){
			layer.msg("设置成功",{time:1500},layerClose)
		}else{
			layer.msg("设置失败")
		}
	})
</script>
</body>
</html>